$(()=>{
    let this_id;
    let count;
    let numList = ["一","二","三"];
    let topProfileList = ["{IMG_svg/undraw-cat-re-gkh9_svg}","{IMG_svg/undraw-dog-re-7980_svg}","{IMG_svg/undraw-cat-re-gkh9_svg}"];
    let profileList = ["{IMG_svg/undraw-male-avatar-re-y880_svg}","{IMG_svg/undraw-pic-profile-re-7g2h_svg}","{IMG_svg/undraw-profile-pic-re-iwgo_svg}"];
    for (let i=0;i<3;i++){
        $("div.group")[i].id=`group${i+1}`;
        $(`div#group${i+1}>div.top>div.cardTitle`).text(`第${numList[i]}组`);
        $(`div#group${i+1}>img`).attr("src",topProfileList[i]);
        $(`div#group${i+1}>div.top>img`).attr("src",profileList[i]);
        for (let j=0;j<3;j++){
            $(`div#group${i+1}>div.container>div.list>div.listItem>img`)[j].id=`g${i+1}i${j+1}`;
            $(`div#group${i+1}>div.container>div.resultList>div.result`)[j].id=`result-g${i+1}i${j+1}`;
        }
    }
    $("#choose1").addClass("active");
    $("#group2").hide();
    $("#group3").hide();
    $(".listItem>img").click(function (){
        this_id = this.id;
        $("#file").click();
    });
    $("#file").change(()=>{
        let formData = new FormData();
        formData.append("file",$("#file")[0].files[0]);
        formData.append("tag",this_id);

        $.ajax({
            url: "{LINK_uploadIMG}",
            type: "post",
            data: formData,
            processData: false,
            contentType: false,
            dataType: 'text',
            success: text=>{
                let data = JSON.parse(text);
                $("#file").val("");
                if(data.status){
                    // alert("上传成功");
                    swal("上传成功","上传成功","success",{timer:2000});
                    refreshIMG();
                }else{
                    if (data.hacker){
                        console.log("Hacker:"+data.hacker);
                    }
                    swal("上传失败",data.msg,"error",{timer:2000});
                    // alert("上传失败："+data.msg);
                }
                // console.log(text);
            }
        });
        return true;
    });
    const refreshIMG = ()=>{
        for (let i=1;i<4;i++){
            for (let j=1;j<4;j++){
                let $img = $(`img#g${i}i${j}`);
                $img.attr("src","{IMG_blank_jpg}");
                setTimeout(()=>{
                    $img.attr("src",`{LINK_img}?tag=g${i}i${j}`);
                },10);
            }
        }
    }
    const getVote = ()=>{
        $.post("{LINK_getVoteAll}",{},text=>{
            let data=JSON.parse(text);
            for (let i=0;i<3;i++){
                let a = data[i];
                for (let j=0;j<3;j++){
                    let b = a[j];
                    $(`#result-g${i+1}i${j+1}`).text(b);
                }
            }
        });
    }
    counter();
    function counter(){
        $.get("{LINK_getCounter}",text=>{
            if (count!==text){
                count=text;
                getVote();
                refreshIMG();
            }
            setTimeout(counter,500);
        });
    }

    $("#clear").click(()=>{
        swal("是否要清除","将清除已上传的作品以及投票数",{
            buttons:{
                a:{
                    text:"取消",
                    value: "cancel"
                },
                b:{
                    text:"确定",
                    value: "ok"
                },
                cancel:{
                    value: "cancel"
                }
            }
        }).then(value=>{
            if (value==="ok"){
                $.post("{LINK_clear}",{},text=>{
                    let data = JSON.parse(text);
                    if (data.status) {
                        swal("清空成功","","success",{timer:2000});
                        // alert("清空成功");
                    }else{
                        swal("清空失败",data.msg,"error",{timer:2000});
                        // alert("清空失败："+data.msg);
                    }
                });
            }
        });
    });
    $("div.choose>div").click(function (){
        let a=$(this).attr("data-group");
        $("div.choose>div").removeClass("active");
        $(this).addClass("active");
        $("div.group").hide();
        $(`#${a}`).show();
    });
    // $("div.listItem>img").mouseover(function (){
    //     $("div.modal>div.main>img").attr("src",$(this).attr("src"));
    //     $("div.modal").show();
    // });
    $("div.modal>div.main>span").click(()=>{
        $("div.modal").hide();
    });

});